package cn.tedu.person.controller;

import cn.tedu.person.pojo.dto.AdminInsertDTO;
import cn.tedu.person.pojo.dto.AdminUpdateDTO;
import cn.tedu.person.pojo.dto.DepartmentInsertDTO;
import cn.tedu.person.pojo.dto.DepartmentUpdateDTO;
import cn.tedu.person.pojo.vo.AdminIndexVO;
import cn.tedu.person.pojo.vo.AdminStandardVO;
import cn.tedu.person.pojo.vo.DepartmentIndexVO;
import cn.tedu.person.pojo.vo.DepartmentStandardVO;
import cn.tedu.person.service.IAdminService;
import cn.tedu.person.service.IDepartmentService;
import cn.tedu.person.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/department")
@Api(tags ="04.部门管理" )
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @ApiOperation("添加部门信息")
    @ApiOperationSupport(order = 100)
    @PostMapping("/addNew")
    public JsonResult insert(@Valid DepartmentInsertDTO departmentInsertDTO){
        System.out.println("DTO="+departmentInsertDTO);
        departmentService.addNew(departmentInsertDTO);
        return JsonResult.ok();
    }

    @ApiOperation("部门信息列表")
    @ApiOperationSupport(order = 200)
    @GetMapping("")
    public JsonResult list(){
        List<DepartmentIndexVO> list = departmentService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("根据Id删除信息")
    @ApiOperationSupport(order = 300)
    @GetMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@Range(min=1,message = "删除失败，ID值无效！")
                             @PathVariable Long id){
        departmentService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据Id查询信息")
    @ApiOperationSupport(order = 400)
    @GetMapping("/{id:[0-9]+}")
    public JsonResult getStandardById(@Range(min=1,message = "查询失败，ID值无效！")
                                      @PathVariable Long id){
        DepartmentStandardVO queryResult = departmentService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @ApiOperation("修改部门信息")
    @ApiOperationSupport(order = 500)
    @PostMapping("/{id:[0-9]+}/updateInfoById")
    public JsonResult update(@Range(min=1,message = "修改失败，ID值无效！")
                             @PathVariable Long id, @Valid DepartmentUpdateDTO departmentUpdateDTO){
        System.out.println("DTO="+departmentUpdateDTO);
        log.debug("DTO={}",departmentUpdateDTO);
        departmentService.updateInfoById(id,departmentUpdateDTO);
        System.out.println("DTO2="+departmentUpdateDTO);
        log.debug("DTO2={}",departmentUpdateDTO);
        return JsonResult.ok();
    }
}
